// 上传word文档解析插入到编辑器中
// 插入文章的自定义菜单
import { IDomEditor, IButtonMenu } from '@wangeditor/editor';
import { FILE_WORD_SVG } from '../../icon-svg';
import { IExtendConfig } from './interface';
import { isUploadDocxDisabled } from '../../isDisabledMenusConfig';


function getUploadDocxConfig(editor: IDomEditor) {
  const { EXTEND_CONF } = editor.getConfig();
  const { uploadDocxConfig } = EXTEND_CONF as IExtendConfig;
  return uploadDocxConfig;
}

class UploadDocxModule implements IButtonMenu {
  tag: string;
  title: string;
  iconSvg: string;
  constructor() {
    this.title = '文档';
    this.iconSvg = FILE_WORD_SVG;
    this.tag = 'button';
  }


  // 菜单是否需要激活（如选中加粗文本，“加粗”菜单会激活），用不到则返回 false
  isActive(editor: IDomEditor): boolean {
    // TS 语法
    return false;
  }

  // 获取菜单执行时的 value ，用不到则返回空 字符串或 false
  getValue(editor: IDomEditor): string | boolean {
    // TS 语法
    return '';
  }

  // 菜单是否需要禁用（如选中 H1 ，“引用”菜单被禁用），用不到则返回 false
  isDisabled(editor: IDomEditor): boolean {
    // TS 语法
    return isUploadDocxDisabled(editor);
  }

  // 点击菜单时触发的函数
  exec(editor: IDomEditor, value: string | boolean) {
    if (this.isDisabled(editor)) return;
    console.log('点击了文档菜单');
    const { showUploadDocxModal } = getUploadDocxConfig(editor);
    showUploadDocxModal(editor);
  }
}

export default UploadDocxModule;
